package com.loop.unclelend.ui.adapter

import android.graphics.Color
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.text.style.UnderlineSpan
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.loop.unclelend.R
import com.loop.unclelend.utils.ImageUtil
import com.loop.unclelend.utils.adapter.UncleRecyclerViewAdapter
import com.loop.unclelend.utils.adapter.UncleViewHolderHelper
import org.json.JSONObject

/**
 * UncleLend
 * @Author wwdeng
 * @CreateTime: 2024/9/18 12:02
 * @Annotation：绑卡适配器
 */
class Adapter_Step_Account(recyclerView: RecyclerView?): UncleRecyclerViewAdapter<JSONObject>(recyclerView) {

    override fun getItemViewType(position: Int): Int {
        val model = getItem(position)
        if (model.optBoolean("isEdit",false)){
            return R.layout.uncle_item_account_edit
        }
        return R.layout.uncle_item_account_index
    }

    override fun setItemChildListener(helper: UncleViewHolderHelper, viewType: Int) {
        helper.setItemChildClickListener(R.id.layout_edit) //点击编辑
        helper.setItemChildClickListener(R.id.img_delete)//删除
    }

    override fun fillData(helper: UncleViewHolderHelper, position: Int, model: JSONObject) {
        if (position == 0){
            val showText = "*${String.format(mContext.getString(R.string.step_bind_card_index),position+1)}"
            val spString = SpannableString(showText)
            spString.setSpan(ForegroundColorSpan(Color.parseColor("#FFFF4D3E")), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
            helper.setText(R.id.txt_index,spString)
        } else {
            helper.setText(R.id.txt_index,String.format(mContext.getString(R.string.step_bind_card_index),position+1))
        }


        //是否是编辑
        val isEdit = model.optBoolean("isEdit",true)
        if (!isEdit){
            helper.setText(R.id.txt_title,model.optString("walletDesc"))
            helper.setText(R.id.txt_account,"+" + model.optString("walletMobile"))

            val imgLogo = helper.getImageView(R.id.img_logo)
            ImageUtil.instance!!.loadImageUrl(mContext,imgLogo,model.optString("walletLogo"), R.mipmap.icon_holder)

            //是否显示 删除按钮
            if (model.has("isShowDelete") && !model.optBoolean("isShowDelete")){
                helper.setVisibility(R.id.img_delete, View.INVISIBLE)
            } else {
                helper.setVisibility(R.id.img_delete, View.VISIBLE)
            }
        }
    }
}